package com.liujun.spring4.demo.aop.pointcut.dynamicmethod;

import com.liujun.spring4.demo.aop.pointcut.staticmethod.Waiter;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.support.DynamicMethodMatcherPointcut;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 动态创建切面
 *
 * @author liujun
 * @version 0.0.1
 * @date 2018/09/26
 */
public class DynmicMethodMatcherPointCut extends DynamicMethodMatcherPointcut {

  private static List<String> specialClientList = new ArrayList<>(2);

  static {
    specialClientList.add("feifei");
    specialClientList.add("kk");
  }

  @Override
  public ClassFilter getClassFilter() {
    return new ClassFilter() {
      @Override
      public boolean matches(Class<?> clazz) {
        System.out.println("调用getClassFilter对" + clazz.getName() + "做类级别的静态切点检查");
        return Waiter.class.isAssignableFrom(clazz);
      }
    };
  }

  @Override
  public boolean matches(Method method, Class<?> targetClass) {
    System.out.println(
        "调用matches(method,class)," + targetClass.getName() + "." + method.getName() + "做方级别的静态检查");
    return "greetTo".equals(method.getName());
  }

  @Override
  public boolean matches(Method method, Class<?> targetClass, Object... args) {
    System.out.println(
        "调用matches(method,class,...args),"
            + targetClass.getName()
            + "."
            + method.getName()
            + "做方级别的动态检查");
    String clientName = String.valueOf(args[0]);
    return specialClientList.contains(clientName);
  }
}
